import 'package:flutter/material.dart';
import 'package:helloworld/tabs.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'dart:ui';

class AddGoal extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AddGoal();
  }
}

class _AddGoal extends State<AddGoal> {
  DateTime _dateData;
  Color _textColor;
  Color _finishedColor = Colors.white54;
  Color _unFinishColor = Colors.white54;
  var _date = TextEditingController();
  var _contextData = TextEditingController();
  var _oathData = TextEditingController();
  var _finishData = TextEditingController();
  Brightness brightnessValue;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _dateData = DateTime.now();
    _date.text = '${formatDate(_dateData, [yyyy, '年', mm, '月', dd, '日'])}';
    _finishData.text = 'no';
    brightnessValue = window.platformBrightness;
    if(window.platformBrightness==Brightness.dark){
      _textColor = Colors.white54;
    }else{
      _textColor = Colors.black54;
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('立下目标'),
        actions: <Widget>[
          InkWell(
            child: Padding(
              child: Text(
                '保存',
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              padding: EdgeInsets.all(16),
            ),
            onTap: () {
              Dio().post("http://123.207.114.127:1314/goal/addGoal",queryParameters: {
                'goaltime':'${formatDate(_dateData, [yyyy,'年',mm, '月',dd ,'日'])}',
                'goalcontent':_contextData.text,
                'goaloath':_oathData.text,
                'goalfinish':_finishData.text
              });
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:4)), (route) =>route==null);
              });
            },
          )
        ],
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              InkWell(
                child: TextField(
                  controller: _date,
                  enabled: false,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(), labelText: '预计日期'),
                ),
                onTap: () {
                  DatePicker.showDatePicker(
                    context,
                    onMonthChangeStartWithFirstDate: true,
                    pickerTheme: DateTimePickerTheme(
                      showTitle: true,
                      confirm: Text('确定',
                          style: TextStyle(color: Colors.black54, fontSize: 20)),
                      cancel: Text('取消',
                          style: TextStyle(color: Colors.black54, fontSize: 20)),
                    ),
                    minDateTime: DateTime.parse('2020-01-01'),
                    maxDateTime: DateTime.parse('2099-12-31'),
                    initialDateTime: _dateData,
                    dateFormat: 'yyyy-MMMM-dd',
                    locale: DateTimePickerLocale.zh_cn,
                    onConfirm: (dateTime, List<int> index) {
                      setState(() {
                        _dateData = dateTime;
                        _date.text = '${formatDate(_dateData, [
                          yyyy,
                          '年',
                          mm,
                          '月',
                          dd,
                          '日'
                        ])}';
                      });
                    },
                  );
                },
              ),
              SizedBox(
                height: 10,
              ),
              TextField(
                keyboardAppearance: brightnessValue,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: '目标内容'),
                onChanged: (value) {
                  setState(() {
                    _contextData.text = value;
                  });
                },
              ),
              SizedBox(
                height: 10,
              ),
              TextField(
                keyboardAppearance: brightnessValue,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: '宣言'),
                onChanged: (value) {
                  setState(() {
                    _oathData.text = value;
                  });
                },
              ),
              SizedBox(height: 20,),
              Container(
                decoration: BoxDecoration(
                    border: Border.all(width: 1,color: Colors.grey),
                    borderRadius: BorderRadius.circular(4)
                ),
                height: 60,
                child: Row(
//                mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(width: 10,),
                    Text('是否完成',style: TextStyle(fontSize: 16,color: _textColor),),
                    SizedBox(width: 20,),
                    RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      color: _finishedColor,
                      child: Text('已完成',
                          style: TextStyle(fontSize: 20, color: Colors.black54)),
                      onPressed: () {
                        setState(() {
                          _finishData.text = 'yes';
                          _finishedColor = Colors.lightBlue;
                          _unFinishColor = Colors.white54;
                        });
                      },
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      color: _unFinishColor,
                      child: Text('未完成',
                          style: TextStyle(fontSize: 20, color: Colors.black54)),
                      onPressed: () {
                        setState(() {
                          _finishData.text = 'no';
                          _unFinishColor = Colors.lightBlue;
                          _finishedColor = Colors.white54;
                        });
                      },
                    )
                  ],
                ),
              )
            ],
          ),
          scrollDirection: Axis.vertical,
        ),
      ),
    );
  }
}
